package com.dabai.dbutils.utils;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Build;

/**
 * Description : Other
 *
 * @author BAI
 */
public class ShortcutsUtils {

    @SuppressLint("UnspecifiedImmutableFlag")
    public static void addShortCut(Context context, String name, int icon, Intent intent) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
            if (shortcutManager.isRequestPinShortcutSupported()) {
                intent.setAction(Intent.ACTION_VIEW); //action必须设置，不然报错
                ShortcutInfo info;
                info = new ShortcutInfo.Builder(
                        context,
                        "The only id" + (int) (Math.random() * 10000)
                )
                        .setIcon(Icon.createWithResource(context, icon))
                        .setShortLabel(name)
                        .setIntent(intent)
                        .build();

                //当添加快捷方式的确认弹框弹出来时，将被回调
                PendingIntent shortcutCallbackIntent =
                        PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());
            }
        }else {
            DabaiUtils.showToast(context,"当前系统不支持快捷方式!");
        }
    }




}
